//
//  AMapViewController.m
//  CooperLinker
//
//  Created by 张 晓军 on 16/4/5.
//  Copyright © 2016年 洪雯. All rights reserved.
//

#import "AMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#define WINSIZE [[UIScreen mainScreen] bounds]
#define TextFiledTag 2000
#define Form1 @"5711e50e7bbf1959a515fcfd"

@interface AMapViewController ()<AMapSearchDelegate>
{
    UIView *_view;
    AMapSearchAPI *_search; //地图查询
}

@property (nonatomic,copy)NSString *keyWord;
@end

@implementation AMapViewController
#pragma mark viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
    [UIView animateWithDuration:0.5 animations:^{
        _view.frame = CGRectMake(_view.frame.origin.x, 70, _view.frame.size.width, _view.frame.size.height);
//        _view.backgroundColor = [UIColor yellowColor];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //搜索框
//    UIButton *titlebtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 44, WINSIZE.size.width - 40, 40)];
//    [self.view addSubview:titlebtn];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 22, 30, 40)];
    backButton.backgroundColor = [UIColor orangeColor];
    [backButton addTarget:self action:@selector(BackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    UITextField *titleLable = [[UITextField alloc] initWithFrame:CGRectMake(40, 22, WINSIZE.size.width - 50, 40)];
    titleLable.tag = TextFiledTag;
    titleLable.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:titleLable];
    
    UIButton *btnDropbox = [[UIButton alloc] initWithFrame:CGRectMake(WINSIZE.size.width -50, 22, 40, 40)];
    btnDropbox.backgroundColor = [UIColor orangeColor];
    [btnDropbox addTarget:self action:@selector(KeyWordSeach:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnDropbox];

    
    [self creatView];
//    [self initSeachMap];
    
}

#pragma mark 创建弹出按钮
- (void)creatView {
    _view = [[UIView alloc] initWithFrame:CGRectMake(0, WINSIZE.size.height, WINSIZE.size.width, 120)];
    [self.view addSubview:_view];
    
    float numX = (WINSIZE.size.width ) / 4.0;
    float orgX = (numX / 2.0 - 20);
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(orgX+numX*i, 0, 40, 40)];
        btn.backgroundColor = [UIColor magentaColor];
        [btn.layer setMasksToBounds:YES];
        btn.layer.cornerRadius = 20.0;
        [_view addSubview:btn];
    }
}
#pragma mark 返回按钮
- (void)BackClick {
    [self dismissViewControllerAnimated:NO completion:nil];
}
#pragma mark 关键字查询
- (void)KeyWordSeach:(UIButton *)sender {
    UITextField *textFiled = (UITextField *)[self.view viewWithTag:TextFiledTag];
    self.keyWord = textFiled.text;
    [self initSeachMap];

}

#pragma mark seach请求
- (void)initSeachMap {
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    //构造AMapCloudPOIAroundSearchRequest对象，设置云周边检索请求参数
    AMapCloudPOIAroundSearchRequest *request = [[AMapCloudPOIAroundSearchRequest alloc] init];
    request.tableID = Form1;//在数据管理台中取得
    request.center = [AMapGeoPoint locationWithLatitude:39.594494 longitude:118.15526];
    request.radius = 3000;
    request.keywords = self.keyWord;
    request.offset = 20;
    
    //发起云周边搜索
    [_search AMapCloudPOIAroundSearch: request];
    
    AMapCloudPOIAroundSearchRequest *requestT = [[AMapCloudPOIAroundSearchRequest alloc] init];
    requestT.tableID = Form1;//在数据管理台中取得
    requestT.center = [AMapGeoPoint locationWithLatitude:39.601905 longitude:118.169924];
    requestT.radius = 3000;
    self.keyWord = self.keyWord;
    requestT.offset = 20;
    
    //发起云周边搜索
    [_search AMapCloudPOIAroundSearch: requestT];

    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 实现云检索对应的回调函数
//实现云检索对应的回调函数
- (void)onCloudSearchDone:(AMapCloudSearchBaseRequest *)request response:(AMapCloudPOISearchResponse *)response
{
    NSLog(@">>>====<<<<%lu",(unsigned long)response.POIs.count);
//    //获取云图数据并显示
//    if(response.POIs.count == 0)
//    {
//        return;
//    }else {
//        for (int i = 0; i < response.POIs.count; i++) {
//            AMapCloudPOI *arryInfo = response.POIs[i];
//            NSLog(@"===%ld",(long)arryInfo.uid);
//            NSLog(@"===%@",arryInfo.name);
//            NSLog(@"===%@",arryInfo.address);
//            NSLog(@"===%@",arryInfo.location);
//            NSLog(@"===%@",arryInfo.images);
//            NSLog(@"离当前距离===%ld",(long)arryInfo.distance);
//
//
//        }
//        
//        
//    }
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
